package com.antler.smt.admin.service.impl;

import com.antler.smt.admin.mapper.PurchaserAccountInfoMapper;
import com.antler.smt.admin.mapper.PurchaserChargeEntityMapper;
import com.antler.smt.admin.mapper.PurchaserChargeInfoMapper;
import com.antler.smt.admin.mapper.PurchaserEntityMapper;
import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.entity.MemberChargeEntity;
import com.antler.smt.entity.PurchaserAccountEntity;
import com.antler.smt.entity.PurchaserChargeEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.PurchserAccountReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.MemberChargeService;
import com.antler.smt.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员账户
 *
 * @author zhangyi
 * @date 2018/4/8 14:44
 */
@Service
public class MemberChargeServiceImpl implements MemberChargeService {

    @Autowired
    private PurchaserChargeEntityMapper purchaserChargeEntityMapper;

    @Autowired
    private PurchaserAccountInfoMapper purchaserAccountInfoMapper;

    /**
     * 获取会员充值列表
     *
     * @return
     */
    @Override
    public Page<MemberChargeEntity> getMemberChargeList() {
        return null;
    }

    /**
     *  审核会员充值信息
     *
     * @param id
     * @param auditResult
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result<ResultCodeEnum> updateMemberCharge(long id, Integer auditResult, Long userId,Long actualAmmount) {
        Result result = null;

        //创建采购商充值实体
        PurchaserChargeEntity purchaserChargeEntity = new PurchaserChargeEntity();

        //插入id
        purchaserChargeEntity.setId(id);

        //插入审核结果
        purchaserChargeEntity.setAuditResult(auditResult);

        //插入审核用户id
        purchaserChargeEntity.setAuditUserId(userId);

        //插入实际审核的金额
        purchaserChargeEntity.setActualAmmount(actualAmmount);

        //插入审核时间
        purchaserChargeEntity.setAuditTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //插入更新时间
        purchaserChargeEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        int i = purchaserChargeEntityMapper.updateByPrimaryKeySelective(purchaserChargeEntity);

        if(0 == i){
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR,"采购商充值金额不符！！");
        }else{
            //创建采购商账户请求实体
            PurchserAccountReq purchserAccountReq = new PurchserAccountReq();

            //充值实体
            PurchaserChargeEntity purchaserCharge = purchaserChargeEntityMapper.selectByPrimaryKey(id);

            //会员id
            Long memberId = purchaserCharge.getMemberId();

            //更改采购商账户里面的余额
            PurchaserAccountEntity purchaserAccountEntity =
                    purchaserAccountInfoMapper.selectPurchaserAccountByMemberId(memberId);

            //此时账户里面的余额
            Long moneyLeft = purchaserAccountEntity.getMoneyLeft();

            // 账户里余额+实际审核金额
            purchserAccountReq.setMoneyLeft(moneyLeft+purchaserCharge.getActualAmmount());

            purchserAccountReq.setMemberId(memberId);

            int i1 = purchaserAccountInfoMapper.updatPurchaserAccount(purchserAccountReq);

            //升级星级等级


            if((i1 & i) == 0){
                result = new Result(ResultCodeEnum.APPEND_DATA_ERROR,"采购商修改余额错误！！");
            }else{
                result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
            }
        }

        return result;
    }


}

